//
//  MXSUserAccount.swift
//  weibo
//
//  Created by 孙飞 on 15/12/9.
//  Copyright © 2015年 Sunfei. All rights reserved.
//

import UIKit


// ******** q请求数据的根节点，项目所用所有数据的源

class MXSUserAccount: NSObject, NSCoding {
    
    //id
    var uid: String?
    // ******** 过期时间,单位秒 --> JSON 里面的值没有用 引号引起来
    var expires_in: NSTimeInterval = 0 {
        didSet{
            //******** 在外界给 expires_in 赋值的时候,就可以去计算出来具体什么时候过期
            expiresDate = NSDate(timeIntervalSinceNow: expires_in)
            print(expiresDate)
        }
    }
    
    var expiresDate: NSDate?    // 什么时候过期 accessToken 获取回来的那个时间点,加上过期的秒数

    var access_token: String?    // 用于调用access_token，接口获取授权后的access token

    var avatar_large: String?    // 用户的头像地址

    var screen_name: String?    // 用户的昵称

    
    init(dict: [String: AnyObject]){
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    override var description: String {
        let keys = ["uid", "expires_in", "access_token", "avatar_large", "screen_name"]
        return dictionaryWithValuesForKeys(keys).description
    }
    
    // MARK: -  归档的协议方法
    required init(coder aDecoder: NSCoder) {
        
        uid = aDecoder.decodeObjectForKey("uid") as? String
        expiresDate = aDecoder.decodeObjectForKey("expiresDate") as? NSDate
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
    }
    
    //归档
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(expiresDate, forKey: "expiresDate")
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
    }
    
}
